package com.iseastar.guojiang.newcabinet;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.model.Const;
import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.MainActivity;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ParcelTransferPointBean;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.ServiceTaskBean;
import com.shouhuobao.station.R;
import com.shouhuobao.take.utils.AppTab;

import droid.frame.ui.utils.Utils;
import droid.frame.view.ViewPagerExt;

/**
 * Created by joker on 2017/12/19.
 * 任务详情页面
 */
public class TaskMainDetailActivity extends BaseActivity2 implements View.OnClickListener {

    public BaseFragment mFragment1, mFragment2;
    private TextView mRightTV;
    private ServiceTaskBean serviceTaskBean = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        serviceTaskBean = (ServiceTaskBean) getIntent().getSerializableExtra("item");
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_task_detail_main);
        super.findViewById();
        findViewById(R.id.title_left_image).setOnClickListener(this);
        mRightTV = (TextView) findViewById(R.id.title_right_text);
        mRightTV.setOnClickListener(this);
        TextView[] mTags = new TextView[2];
        mTags[0] = (TextView) findViewById(R.id.task_tab1);
        mTags[1] = (TextView) findViewById(R.id.task_tab2);
        initFragment();
        ViewPagerExt mViewPager = (ViewPagerExt) findViewById(R.id.viewpager);
        ImageView mImgLine = (ImageView) findViewById(R.id.task_line_img);
        mViewPager.setFragmentItems(getSupportFragmentManager(), mFragment1, mFragment2);
        mViewPager.setSupportScroll(true);
        AppTab appTab = new AppTab(getContext(), 0);
        appTab.setWidthMargin(Utils.dpToPx(150, getResources()));
        appTab.setTextColor(getResources().getColor(R.color.white), Color.parseColor("#D1D1D6"));
        appTab.initPosition(mViewPager, mImgLine, mTags);

        mViewPager.setCurrentItem(0, true);

        if (null != serviceTaskBean) {
            //回仓交接不显示任务异常
            if (Const.CourierTaskStatus.transferTask == serviceTaskBean.getTaskType()) {
                mRightTV.setVisibility(View.INVISIBLE);
            }
            //驿站，柜子，上门显示任务异常
            else if (Const.CourierTaskStatus.stationTask == serviceTaskBean.getTaskType()
                    || Const.CourierTaskStatus.visitTask == serviceTaskBean.getTaskType()
                    || Const.CourierTaskStatus.cabinetTask == serviceTaskBean.getTaskType()) {
                mRightTV.setVisibility(View.VISIBLE);
            } else {//同城
                mRightTV.setVisibility(View.INVISIBLE);
            }
        }
    }

    private void initFragment() {
        mFragment1 = new TaskStageFragment();
        Bundle bundle1 = new Bundle();
        bundle1.putInt("type", 0);
        bundle1.putSerializable("item", serviceTaskBean);
        mFragment1.setArguments(bundle1);

        mFragment2 = new TaskDetailFragment();
        Bundle bundle2 = new Bundle();
        bundle2.putInt("type", 1);
        bundle2.putSerializable("item", serviceTaskBean);
        mFragment2.setArguments(bundle2);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            //返回上一页
            case R.id.title_left_image:
                finish();
                break;
            //任务异常
            case R.id.title_right_text:
                if (null == serviceTaskBean) {
                    return;
                }
                intent = new Intent(getContext(), TaskExceptionActivity.class);
                intent.putExtra("item", serviceTaskBean);
                startActivityForResult(intent, 1100);
                break;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            int type = bundle.getInt("type");
            ServiceTaskBean item = null;
            ParcelTransferPointBean item1 = null;
            if (type == 1) {
                item = (ServiceTaskBean) bundle.getSerializable("item");
            } else {
                item1 = (ParcelTransferPointBean) bundle.getSerializable("item");
            }
            MainActivity.instance.refreshHomeData();
            if (item != null) {
                serviceTaskBean.setLatitude(item.getLatitude());
                serviceTaskBean.setLongitude(item.getLongitude());
                serviceTaskBean.setGdLatitude(item.getGdLatitude());
                serviceTaskBean.setGdLongitude(item.getGdLongitude());
                serviceTaskBean.setDistance1(item.getDistance1());
                serviceTaskBean.setContactName(item.getContactName());
                serviceTaskBean.setPhone(item.getPhone());
                serviceTaskBean.setTimeMessage(item.getTimeMessage());
                serviceTaskBean.setTransStart(item.getTransStart());
                serviceTaskBean.setTransEnd(item.getTransEnd());
                serviceTaskBean.setWareHouseName(item.getWareHouseName());
                ((TaskStageFragment) mFragment1).refreshDataToView(serviceTaskBean);
                ((TaskDetailFragment) mFragment2).refreshDataToView(serviceTaskBean);
            } else if (item1 != null) {
                serviceTaskBean.setLatitude(item1.getBdLatitude());
                serviceTaskBean.setLongitude(item1.getBdLongitude());
                serviceTaskBean.setGdLatitude(item1.getGdLatitude());
                serviceTaskBean.setGdLongitude(item1.getGdLongitude());
                serviceTaskBean.setDistance1(item1.getDistance());
                serviceTaskBean.setContactName(item1.getCourierNickName());
                serviceTaskBean.setPhone(item1.getCourierPhone());
                serviceTaskBean.setTimeMessage(item1.getTimeMessage());
                serviceTaskBean.setTransStart(item1.getTransStart());
                serviceTaskBean.setTransEnd(item1.getTransEnd());
                serviceTaskBean.setWareHouseName(item1.getWareHouseName());
                ((TaskStageFragment) mFragment1).refreshDataToView(serviceTaskBean);
                ((TaskDetailFragment) mFragment2).refreshDataToView(serviceTaskBean);
            }
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //完成揽收包裹返回,如果成功就销毁当前的类
            case MsgID.courier_parcel_list_take_data:
                ReqResult<?> finishResult = JSON.parser(msg.obj);
                if (checkLoginStatus(finishResult)) {
                    if ((Boolean) finishResult.getResult()) {
                        finish();
                    }
                }
                return false;
            default:
                return super.handleMessage(msg);
        }
    }
}
